<?php

/**
 * Name: 微信用户仓库层
 * User: Silent
 * Date: 2025-09-20
 * Time: 22:44:45
 */

namespace App\Repositories\Auth;

use App\Models\Auth\WechatUser;
use App\Repositories\BaseRepository;

class WechatUserRepository extends BaseRepository
{
    public function __construct(WechatUser $model)
    {
        parent::__construct($model);
    }

    /**
     * 根据openid查询用户信息
     * @param string $openid
     * @return mixed
     */
    public function findByOpenid(string $openid)
    {
        return $this->model->with('employee')->where('openid', $openid)->first();
    }
    
    /**
     * 根据openid查找微信用户并关联员工信息
     *
     * @param int $userId
     * @return WechatUser|null
     */
    public function findByOpenidWithEmployee(int $userId)
    {
        return $this->model->with('employee')->where('id', $userId)->first();
    }

    /**
     * 根据unionid查找微信用户
     *
     * @param string $unionid
     * @return WechatUser|null
     */
    public function findByUnionid(string $unionid)
    {
        return $this->model->where('unionid', $unionid)->first();
    }

    /**
     * 创建或更新微信用户
     *
     * @param array $data
     * @return WechatUser
     */
    public function createOrUpdate(array $data)
    {
        $openid = $data['openid'] ?? null;
        
        if (!$openid) {
            throw new \InvalidArgumentException('openid is required');
        }

        $user = $this->findByOpenid($openid);
        
        if ($user) {
            $user->update($data);
            return $user;
        }

        return $this->create($data);
    }

    /**
     * 更新用户信息
     *
     * @param string $openid
     * @param array $data
     * @return bool
     */
    public function updateByOpenid(string $openid, array $data)
    {
        return $this->model->where('openid', $openid)->update($data);
    }

    /**
     * 获取微信用户列表（分页）
     *
     * @param array $filters
     * @param int $perPage
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function index(array $filters = [], int $perPage = 15)
    {
        $query = $this->query();

        // 根据昵称搜索
        if (!empty($filters['nickname'])) {
            $query->where('nickname', 'like', '%' . $filters['nickname'] . '%');
        }

        // 根据性别筛选
        if (isset($filters['sex']) && $filters['sex'] !== '') {
            $query->where('sex', $filters['sex']);
        }

        // 根据城市筛选
        if (!empty($filters['city'])) {
            $query->where('city', 'like', '%' . $filters['city'] . '%');
        }

        // 根据省份筛选
        if (!empty($filters['province'])) {
            $query->where('province', 'like', '%' . $filters['province'] . '%');
        }

        // 根据创建时间筛选
        if (!empty($filters['start_date'])) {
            $query->where('created_at', '>=', $filters['start_date']);
        }

        if (!empty($filters['end_date'])) {
            $query->where('created_at', '<=', $filters['end_date']);
        }

        return $query->orderBy('created_at', 'desc')->paginate($perPage);
    }

    /**
     * 统计微信用户数量
     *
     * @param array $filters
     * @return int
     */
    public function count(array $filters = [])
    {
        $query = $this->query();

        if (!empty($filters['sex'])) {
            $query->where('sex', $filters['sex']);
        }

        if (!empty($filters['city'])) {
            $query->where('city', 'like', '%' . $filters['city'] . '%');
        }

        return $query->count();
    }
}